package com.hdoit.ares.logical.tp.zzb.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.hdoit.ares.logical.cust.model.Customer;
import com.hdoit.ares.logical.cust.service.ICustomerService;
import com.hdoit.ares.logical.insu.model.FanhuaCar;
import com.hdoit.ares.logical.tp.zzb.dao.ITomeasureDao;
import com.hdoit.ares.logical.tp.zzb.model.Insbregion;
import com.hdoit.ares.logical.tp.zzb.model.Prvname;
import com.hdoit.ares.logical.tp.zzb.model.Tomeasure;
import com.hdoit.ares.logical.tp.zzb.model.ZzbDataCallback;
import com.hdoit.ares.logical.tp.zzb.service.IInsbregionService;
import com.hdoit.ares.logical.tp.zzb.service.IPrvnameService;
import com.hdoit.ares.logical.tp.zzb.service.ITomeasureService;
import com.hdoit.ares.logical.viewmodel.tp.zzp.TomeasureSearch;
import com.hdoit.ares.selfcommon.enums.brokerage.ConfirmState;
import com.hdoit.ares.selfcommon.enums.tp.ComState;
import com.tennetcn.common.dao.base.impl.SuperService;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.message.LoginModel;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.DateUtils;
import com.xiaoleilu.hutool.json.JSONUtil;
/**
 * @author      caochengmeng
 * @email       
 * @createtime  2018年03月01日 14:31:37
 * @comment
 */
@Component
public class TomeasureServiceImpl extends SuperService<Tomeasure> implements ITomeasureService{

	private ITomeasureDao tomeasureDao;
	
	public ITomeasureDao getTomeasureDao() {
		return tomeasureDao;
	}
	@Resource
	public void setTomeasureDao(ITomeasureDao tomeasureDao) {
		this.tomeasureDao = tomeasureDao;
	}
	
	private ICustomerService customerService;
	private IPrvnameService prvnameService;
	private IInsbregionService insbregionService;
	
	public ICustomerService getCustomerService() {
		return customerService;
	}
	@Resource
	public void setCustomerService(ICustomerService customerService) {
		this.customerService = customerService;
	}

	public IPrvnameService getPrvnameService() {
		return prvnameService;
	}
	@Resource
	public void setPrvnameService(IPrvnameService prvnameService) {
		this.prvnameService = prvnameService;
	}

	public IInsbregionService getInsbregionService() {
		return insbregionService;
	}
	@Resource
	public void setInsbregionService(IInsbregionService insbregionService) {
		this.insbregionService = insbregionService;
	}


	@Override
	public boolean updateTomeasure(String id, LoginModel loginModel) {
		Tomeasure tomeasure = this.queryModel(id);
		//避免并发操作
		if(ConfirmState.confirmed.equals(tomeasure.getConfirmState())){
			return false;
		}
		tomeasure.setConfirmState(ConfirmState.confirmed);
		tomeasure.setModelStatus(ModelStatus.update);
		System.err.println("进入改变状态");
		if(this.applyChange(tomeasure)){
			//向用户推送微信公众号消息
			return true;
		}
		return false;
	}

	@Override
	public List<Tomeasure> queryList(PagerModel pagerModel,TomeasureSearch search){
		return tomeasureDao.queryList(pagerModel,search);
	}
	
	@Override
	public boolean setTomeasure(FanhuaCar fanhuaCar,ZzbDataCallback zzbDataCallback){
		Tomeasure tomeasure = new Tomeasure();
		tomeasure.setId(CommonUtils.getKeyId());
		tomeasure.setCustomerId(zzbDataCallback.getChannelUserId());
		
		tomeasure.setTaskId(zzbDataCallback.getTaskId());
		Customer customer = customerService.queryModel(zzbDataCallback.getChannelUserId());
		String name = customer.getUsername();
		if(!StringUtils.isEmpty(name)){
			tomeasure.setName(name);
		}else{
			String userIdentity = String.valueOf(customer.getUserIdentity());
			if(!StringUtils.isEmpty(userIdentity)){
				tomeasure.setName(userIdentity);
			}
		}
		tomeasure.setPhone(customer.getPhone());
		
		tomeasure.setLevel(customer.getLevel());
		tomeasure.setInsuName("品牌车险");
		tomeasure.setCarLicenseNo(fanhuaCar.getCarLicenseNo());
		tomeasure.setCarProperty(fanhuaCar.getCarProperty());
		
		//出单地区
		String prvId = zzbDataCallback.getPrvId();
		System.err.println("prvId*****prvId****prvId"+prvId);
		if(!StringUtils.isEmpty(prvId)){
			
			String a = prvId.substring(0, 4);
			System.err.println("aaaa*a**a*a*a*a*a*a"+a);
			Prvname prvname = prvnameService.queryModel(a);
			String shortName = prvname.getPrvshortname();
			String b = prvId.substring(4, 8);
			System.err.println("bbbbbb*b*b*b*b**b*bb"+b);
			String c = b + "00";
			System.err.println("cc+cc+c+sc+sacascccc"+c);
			Insbregion insbregion = insbregionService.queryModelByComcode(c);
			String comcodename = insbregion.getComcodename();
			String insuPlace = comcodename + shortName;
			tomeasure.setInsuPlace(insuPlace);
		}
		
		//评分参数
		String scoreRateStr = zzbDataCallback.getScoreRate();
		System.err.println("scoreRateStr"+scoreRateStr);
		if(!StringUtils.isEmpty(scoreRateStr)){
			Map<String, Object> scoreRateMap = JSONUtil.parseObj(scoreRateStr);
			System.err.println("scoreRateMap"+scoreRateMap);

			Integer bizScore = MapUtils.getInteger(scoreRateMap, "bizScore");
			System.err.println("bizScore****bizScore"+bizScore);
			
			String bizRate = MapUtils.getString(scoreRateMap, "bizRate");
			System.err.println("bizRate****bizRate"+bizRate);
			if(!StringUtils.isEmpty(bizScore)){
				String bizScore1 = String.valueOf(bizScore);
				System.err.println("bizScore1"+bizScore1);
				tomeasure.setScoreRate(bizScore1);
			}else if(!StringUtils.isEmpty(bizRate)){
				tomeasure.setScoreRate(bizRate);
			}else{
				tomeasure.setScoreRate("");
			}
		}
		

		//添加金额信息
		tomeasure.setBusinessMoney(fanhuaCar.getBizInsureInfoPremium());
		tomeasure.setEfcInsureMoney(fanhuaCar.getEfcInsureInfoPremium());
		String taxInsureInfoLateFee = fanhuaCar.getTaxInsureInfoTaxFee();
		String taxInsureInfoTaxFee = fanhuaCar.getTaxInsureInfoTaxFee();
		if(!StringUtils.isEmpty(taxInsureInfoLateFee)){
			double taxInsureInfoLateFee1 = Double.valueOf(taxInsureInfoLateFee);
			if(!StringUtils.isEmpty(taxInsureInfoTaxFee)){
				double taxInsureInfoTaxFee1 = Double.valueOf(taxInsureInfoTaxFee);
				double taxMoney1 = taxInsureInfoLateFee1 + taxInsureInfoTaxFee1;
				String taxMoney = String.valueOf(taxMoney1);
				tomeasure.setTaxMoney(taxMoney);
			}
			String taxMoney = String.valueOf(taxInsureInfoLateFee1);
			tomeasure.setTaxMoney(taxMoney);
		}
		tomeasure.setTotalMoney(fanhuaCar.getTotalPremium());
		
		
		tomeasure.setCreateTime(DateUtils.getCurrent());
		tomeasure.setComState(ComState.no);
		tomeasure.setConfirmState(ConfirmState.unConfirm);
		tomeasure.setDeleteMark("1");
		
		tomeasure.setModelStatus(ModelStatus.add);
		
		if(!applyChange(tomeasure)){
			throw new RuntimeException("tomeasure set error");
		}
		return true;
	}
}
